package com.bookkeeping.cn.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bookkeeping.cn.R;
import com.bookkeeping.cn.model.BookKeepDto;

import java.util.List;

public class BookKeepAdapter extends ArrayAdapter<BookKeepDto> {

    BookKeepItemClick bookKeepItemClick;
    public BookKeepAdapter(@NonNull Context context, int resource, @NonNull List<BookKeepDto> objects) {
        super(context, resource, objects);
    }
    public void setBookKeepItemClick(BookKeepItemClick bookKeepItemClick){
        this.bookKeepItemClick=bookKeepItemClick;
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        BookKeepDto bookKeepDto = getItem(position);//获取当前项的 Fruit 实例
        View view;
        //新增一个内部类 ViewHolder，用于对控件的实例进行缓存
        ViewHolder viewHolder;
        //为每一个子项加载设定的布局
        view = LayoutInflater.from(getContext()).inflate(R.layout.book_keep_item,parent,false);
        viewHolder = new ViewHolder();
        view.setTag(viewHolder);//将 viewHolder 存储在 view 中
        //获取引用
        viewHolder.book_keep_date_tv = view.findViewById(R.id.book_keep_date_tv);
        viewHolder.type_tv = view.findViewById(R.id.type_tv);
        viewHolder.category_tv = view.findViewById(R.id.category_tv);
        viewHolder.price_tv = view.findViewById(R.id.price_tv);
        viewHolder.remark_tv = view.findViewById(R.id.remark_tv);
        viewHolder.btn_delete = view.findViewById(R.id.btn_delete);
        viewHolder.btn_edit = view.findViewById(R.id.btn_edit);

        viewHolder.btn_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bookKeepItemClick.itemClick(0,position);
            }
        });
        viewHolder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bookKeepItemClick.itemClick(1,position);
            }
        });
        //设置参数
        viewHolder.book_keep_date_tv.setText("记账时间："+bookKeepDto.getBookKeepDate());
        viewHolder.type_tv.setText(bookKeepDto.getType().equals(1)?"类型：收入":"类型：支出");
        viewHolder.category_tv.setText("分类："+bookKeepDto.getCategory());
        viewHolder.price_tv.setText("金额："+bookKeepDto.getPrice().toString());
        viewHolder.remark_tv.setText("备注："+bookKeepDto.getRemark());

        return view;

    }

    private class ViewHolder {
        private TextView book_keep_date_tv;
        private TextView type_tv;
        private TextView category_tv;
        private TextView price_tv;
        private TextView remark_tv;

        private Button btn_edit;

        private Button btn_delete;
    }
}
